package cn.tedu.tmall.front.mall.service.impl;


import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.front.mall.dao.persist.repository.IGoodRepository;
import cn.tedu.tmall.front.mall.dao.persist.search.IGoodsSearchRepository;
import cn.tedu.tmall.front.mall.pojo.vo.GoodsListItemVO;
import cn.tedu.tmall.front.mall.pojo.vo.GoodsSearchVO;
import cn.tedu.tmall.front.mall.pojo.vo.GoodsStandardVO;
import cn.tedu.tmall.front.mall.service.IGoodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class GoodServiceImpl implements IGoodService {
    @Value("${tmall.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IGoodRepository goodRepository;
    @Autowired
    private IGoodsSearchRepository searchRepository;

    @Override
    public GoodsStandardVO selectById(Long id) {
        GoodsStandardVO goodsStandardVO = goodRepository.selectById(id);
        if (goodsStandardVO == null) {
            String message = "查询商品失败，该商品不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return goodsStandardVO;
    }

    @Override
    public PageData<GoodsListItemVO> list(Integer pageNum) {
        PageData<GoodsListItemVO> pageData =
                goodRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<GoodsListItemVO> list(Integer pageNum, Integer pageSize) {
        PageData<GoodsListItemVO> pageData =
                goodRepository.list(pageNum, pageSize);
        return pageData;
    }

    @Override
    public PageData<GoodsListItemVO> listByCategoryId(Long categoryId, Integer pageNum) {
        PageData<GoodsListItemVO> pageData =
                goodRepository.listByCategoryId(categoryId, pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<GoodsListItemVO> listByCategoryId(Long categoryId, Integer pageNum, Integer pageSize) {
        PageData<GoodsListItemVO> pageData =
                goodRepository.listByCategoryId(categoryId, pageNum, pageSize);
        return pageData;
    }

    @Override
    public PageData<GoodsSearchVO> customSearch(String keyword, Integer pageNum) {
        PageData<GoodsSearchVO> pageData = searchRepository.customSearch(keyword, pageNum, defaultQueryPageSize);
        return pageData;
    }
}
